﻿using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    public class ConvertModeDAL
    {
        private string DbName;
        public ConvertModeDAL()
        {
            DbName = "LdtCommonManage";
        }

        /// <summary>
        /// 增加ConvertMode表数据
        /// </summary>
        public bool ConvertModeAdd(ConvertMode model)
        {
            string sql = string.Format(@" insert into dbo.ConvertMode(CM_ID,CM_Code,CM_CodeCH,CM_CMode,CM_UpdateTime,CM_Custom,CM_XmlPath,CM_CaseType) values('{0}','{1}','{2}',{3},'{4}','{5}','{6}','{7}') ",
                model.CM_ID, model.CM_Code, model.CM_CodeCH, model.CM_CMode, model.CM_UpdateTime, model.CM_Custom, model.CM_XmlPath, model.CM_CaseType);

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 增加ConvertMode表函数数据
        /// </summary>
        public bool ConvertModeAddHanShu(ConvertMode model)
        {
            string sql = string.Format(@" insert into dbo.ConvertMode(CM_ID,CM_Method,CM_UpdateTime,CM_CMode,CM_MethodParameter,CM_Custom,CM_Code,CM_CodeCH,CM_CaseType) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}') ",
                model.CM_ID, model.CM_Method, model.CM_UpdateTime, model.CM_CMode,model.CM_MethodParameter,model.CM_Custom,model.CM_Code,model.CM_CodeCH, model.CM_CaseType);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 增加ConvertMode表函数数据
        /// </summary>
        public bool ConvertModeAddHanShuConfig(ConvertMode model)
        {
            string sql = string.Format(@" insert into dbo.ConvertMode(CM_ID,CM_Method,CM_UpdateTime,CM_CMode,CM_MethodParameter,CM_XmlPath,CM_CaseType,CM_Custom,CM_Code,CM_CodeCH) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}') ",
                model.CM_ID, model.CM_Method, model.CM_UpdateTime, model.CM_CMode, model.CM_MethodParameter,model.CM_XmlPath,model.CM_CaseType,model.CM_Custom, model.CM_Code, model.CM_CodeCH);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 增加ConvertMode表数据
        /// </summary>
        public bool ConvertModeAddZDConfig(ConvertMode model)
        {
            //string sql = string.Format(@" insert into dbo.ConvertMode(CM_ID,CM_Code,CM_CodeCH,CM_CMode,AT_ID,AT_Code,CM_UpdateTime,CM_XmlPath,CM_CaseType,CM_Custom) values('{0}','{1}','{2}',{3},'{4}','{5}','{6}','{7}','{8}','{9}') ",
            //    model.CM_ID, model.CM_Code, model.CM_CodeCH, model.CM_CMode, model.AT_ID, model.AT_Code, model.CM_UpdateTime, model.CM_XmlPath, model.CM_CaseType, model.CM_Custom);
            string sql = string.Format(@" insert into dbo.ConvertMode(CM_ID,CM_Code,CM_CodeCH,CM_CMode,CM_UpdateTime,CM_XmlPath,CM_Custom,CM_CaseType) values('{0}','{1}','{2}',{3},'{4}','{5}','{6}','{7}') ",
                 model.CM_ID, model.CM_Code, model.CM_CodeCH, model.CM_CMode, model.CM_UpdateTime, model.CM_XmlPath, model.CM_Custom,model.CM_CaseType);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除ConvertMode表数据
        /// </summary>
        public bool ConvertModeDelete(string cmId)
        {
            string sql = string.Format(@" delete from ConvertMode where CM_ID='{0}' and CM_Custom=1 ", cmId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除ConvertMode表数据
        /// </summary>
        public bool ConvertModeDeleteConfig(string cmId)
        {
            string sql = string.Format(@" delete from ConvertMode where CM_ID='{0}' and CM_Custom=0 ", cmId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改ConvertMode表函数数据
        /// </summary>
        public bool ConvertModeHanShuUpdate(ConvertMode model)
        {
            string sql = string.Format(@" update ConvertMode set CM_Method='{0}',CM_UpdateTime='{1}',CM_MethodParameter='{2}',CM_Code='{3}',CM_CodeCH='{4}' where CM_ID='{5}' ",
                model.CM_Method, model.CM_UpdateTime,model.CM_MethodParameter,model.CM_Code,model.CM_CodeCH, model.CM_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改ConvertMode表数据
        /// </summary>
        public bool ConvertModeUpdate(ConvertMode model)
        {
            string sql = string.Format(@" update ConvertMode set CM_Code='{0}',CM_CodeCH='{1}',CM_CMode='{2}',CM_UpdateTime='{3}' where CM_ID='{4}' ",
                model.CM_Code, model.CM_CodeCH, model.CM_CMode, model.CM_UpdateTime, model.CM_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取ConvertMode表数据
        /// </summary>
        public DataTable GetConvertModeData(string cmId)
        {
            string strSql = string.Format(@" select * from ConvertMode where CM_ID ='{0}'  ", cmId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取ConvertMode表数据
        /// </summary>
        public DataTable GetConvertModeHanShuData(string cmId)
        {
            string strSql = string.Format(@" select * from ConvertMode where CM_ID ='{0}' and  CM_CMode in(1,2) ", cmId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }





        /// <summary>
        /// 获取ConvertMode表数据
        /// </summary>
        public DataTable GetConvertModeHanShuData(string xmlPath, string caseType)
        {
            string strSql = string.Format(@" select * from ConvertMode where CM_XmlPath ='{0}' and CM_CaseType='{1}' and  CM_Custom=0 and CM_CMode in(1,2)  ", xmlPath, caseType);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取ConvertMode表数据
        /// </summary>
        public DataTable GetConvertModeData(string xmlPath,string caseType)
        {
            string strSql = string.Format(@" select t1.CM_ID,t1.CM_Code,t1.CM_CodeCH, t2.AR_ID,t2.AT_ID,t2.AT_Code,t2.AR_CaseType,t2.AR_CauseCode,t1.CM_CMode from ConvertMode t1 join ArgumentRel t2 on t1.CM_ID=t2.CM_ID  where CM_XmlPath ='{0}' and CM_CaseType='{1}' and  CM_Custom=0 and CM_CMode=0   ", xmlPath, caseType);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取ConvertMode表数据
        /// </summary>
        public DataTable GetConvertMode(string xmlPath, string caseType)
        {
            string strSql = string.Format(@" select * from ConvertMode where CM_XmlPath ='{0}' and CM_CaseType='{1}' and  CM_Custom=0 ", xmlPath, caseType);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取ConvertMode表数据
        /// </summary>
        public DataTable GetConvertModeDataNew(string cmId)
        {
            string strSql = string.Format(@" select t1.CM_ID,t1.CM_Code,t1.CM_CodeCH, t2.AR_ID,t2.AT_ID,t2.AT_Code,t2.AR_CaseType,t2.AR_CauseCode,t1.CM_CMode,t1.CM_Custom from ConvertMode t1 join ArgumentRel t2 on t1.CM_ID=t2.CM_ID  where  t1.CM_ID ='{0}' ", cmId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

    }
}
